﻿/* ================================================================================
<copyright file="ManifestResource.cs" company="The Framework Factory, LLC" lastWriteTime="12/05/2012 23:23:47 PM">
  Copyright (c) 2012 The Framework Factory, LLC.  All rights reserved.
</copyright>
=================================================================================== */
using System;
using System.IO;
using System.Reflection;

namespace FWF.Logging
{

    /// <summary>
    /// Utility library for interfacing the resources within 
    /// an assembly.
    /// </summary>
    /// <remarks>
    /// Refer to <see cref="System.Reflection.ManifestResourceInfo"/>
    /// </remarks>
    public static class ManifestResource
    {
        /// <summary>
        /// Finds the name of the resource.
        /// </summary>
        /// <param name="refAssembly">The ref assembly.</param>
        /// <param name="find">The find.</param>
        /// <returns></returns>
        public static string FindResourceName( Assembly refAssembly, string find )
        {
            string[] listNames = refAssembly.GetManifestResourceNames();

            string resourceName = string.Empty;

            foreach ( string resource in listNames )
            {
                if ( resource.Contains( find ) )
                {
                    resourceName = resource;
                    break;
                }
            }

            return resourceName;
        }

        /// <summary>
        /// Pull resource from assembly as string.
        /// </summary>
        /// <param name="refAssembly">
        /// Assembly object that contains the resource.
        /// </param>
        /// <param name="resourceName">
        /// Name of resource to pull from assembly.
        /// </param>
        /// <returns>
        /// String from Assembly's Resources.
        /// </returns>
        public static string GetResourceString( Assembly refAssembly, string resourceName )
        {
            if ( refAssembly == null )
            {
                throw new ArgumentNullException( "refAssembly" );
            }

            if ( string.IsNullOrEmpty( resourceName ) )
            {
                throw new ArgumentNullException( "resourceName" );
            }

            using ( Stream objStream = refAssembly.GetManifestResourceStream( resourceName ) )
            {
                if ( objStream == null )
                {
                    return null;
                }

                objStream.Position = 0;

                using ( var objReader = new StreamReader( objStream ) )
                {
                    return objReader.ReadToEnd();
                }
            }
        }

        /// <summary>
        /// Pull resource from assembly as stream.
        /// </summary>
        /// <param name="refAssembly">
        /// Assembly object that contains the resource.
        /// </param>
        /// <param name="resourceName">
        /// Name of resource to pull from assembly.
        /// </param>
        /// <returns>
        /// String from Assembly's Resources.
        /// </returns> 
        public static Stream GetResourceStream( Assembly refAssembly, string resourceName )
        {
            if ( refAssembly == null )
            {
                throw new ArgumentNullException( "refAssembly" );
            }

            if ( string.IsNullOrEmpty( resourceName ) )
            {
                throw new ArgumentNullException( "resourceName" );
            }

            Stream objStream = refAssembly.GetManifestResourceStream( resourceName );

            if ( objStream != null )
            {
                objStream.Position = 0;
                return objStream;
            }
            
            return null;
        }

    }
}
